package com.dm.cloud.auth.auth2;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.oauth2.exception.SaOAuth2Exception;
import com.dm.cloud.core.mode.ResultModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
@Slf4j
public class Oauth2ErrorAdvice {
    @ExceptionHandler(value = {NotLoginException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ResponseEntity<ResultModel>  notLoginException(HttpServletRequest request,
                                                          HttpServletResponse response,
                                                          NotLoginException exception) {
        exception.printStackTrace();
        return ResponseEntity.status(401)
                .body(ResultModel.err(exception.getCode(),exception.getMessage()));
    }
    @ExceptionHandler(value = {SaOAuth2Exception.class})
    @ResponseBody
    public ResponseEntity<ResultModel>  saOAuth2Exception(HttpServletRequest request,
                                                          HttpServletResponse response,
                                                          SaOAuth2Exception exception) {
        exception.printStackTrace();
        return ResponseEntity.status(401)
                .body(ResultModel.err(exception.getCode(),"无效的认证信息"));
    }
}
